home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / image.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.4 KB  |  191 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h> 
  40. #include <aux.h>
  41.  
  42.  
  43. typedef struct _rawImageRec {
  44.     unsigned short imagic;
  45.     unsigned short type;
  46.     unsigned short dim;
  47.     unsigned short sizeX, sizeY, sizeZ;
  48.     unsigned long min, max;
  49.     unsigned long wasteBytes;
  50.     char name[80];
  51.     unsigned long colorMap;
  52.     FILE *file;
  53.     unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  54.     unsigned long rleEnd;
  55.     unsigned long *rowStart;
  56.     long *rowSize;
  57. } rawImageRec;
  58.  
  59.  
  60. static rawImageRec *RawImageOpen(char *fileName)
  61. {
  62.     rawImageRec *raw;
  63.     int x;
  64.  
  65.     raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  66.     if (raw == NULL) {
  67.     fprintf(stderr, "Out of memory!\n");
  68.     auxQuit();
  69.     }
  70.     if ((raw->file = fopen(fileName, "rb")) == NULL) {
  71.     perror(fileName);
  72.     auxQuit();
  73.     }
  74.  
  75.     fread(raw, 1, 12, raw->file);
  76.  
  77.     raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  78.     raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  79.     raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  80.     raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  81.     if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  82.     raw->tmpB == NULL) {
  83.     fprintf(stderr, "Out of memory!\n");
  84.     auxQuit();
  85.     }
  86.  
  87.     if ((raw->type & 0xFF00) == 0x0100) {
  88.     x = raw->sizeY * raw->sizeZ * sizeof(long);
  89.     raw->rowStart = (unsigned long *)malloc(x);
  90.     raw->rowSize = (long *)malloc(x);
  91.     if (raw->rowStart == NULL || raw->rowSize == NULL) {
  92.         fprintf(stderr, "Out of memory!\n");
  93.         auxQuit();
  94.     }
  95.     raw->rleEnd = 512 + (2 * x);
  96.     fseek(raw->file, 512, SEEK_SET);
  97.     fread(raw->rowStart, 1, x, raw->file);
  98.     fread(raw->rowSize, 1, x, raw->file);
  99.     }
  100.     return raw;
  101. }
  102.  
  103. static void RawImageClose(rawImageRec *raw)
  104. {
  105.  
  106.     fclose(raw->file);
  107.     free(raw->tmp);
  108.     free(raw->tmpR);
  109.     free(raw->tmpG);
  110.     free(raw->tmpB);
  111.     free(raw);
  112. }
  113.  
  114. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  115. {
  116.     unsigned char *iPtr, *oPtr, pixel;
  117.     int count;
  118.  
  119.     if ((raw->type & 0xFF00) == 0x0100) {
  120.     fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
  121.     fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
  122.           raw->file);
  123.  
  124.     iPtr = raw->tmp;
  125.     oPtr = buf;
  126.     while (1) {
  127.         pixel = *iPtr++;
  128.         count = (int)(pixel & 0x7F);
  129.         if (!count) {
  130.         return;
  131.         }
  132.         if (pixel & 0x80) {
  133.         while (count--) {
  134.             *oPtr++ = *iPtr++;
  135.         }
  136.         } else {
  137.         pixel = *iPtr++;
  138.         while (count--) {
  139.             *oPtr++ = pixel;
  140.         }
  141.         }
  142.     }
  143.     } else {
  144.     fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  145.           SEEK_SET);
  146.     fread(buf, 1, raw->sizeX, raw->file);
  147.     }
  148. }
  149.  
  150. static void RawImageGetData(rawImageRec *raw, AUX_RGBImageRec *final)
  151. {
  152.     unsigned char *ptr;
  153.     int i, j;
  154.  
  155.     final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  156.     if (final->data == NULL) {
  157.     fprintf(stderr, "Out of memory!\n");
  158.     auxQuit();
  159.     }
  160.  
  161.     ptr = final->data;
  162.     for (i = 0; i < raw->sizeY; i++) {
  163.     RawImageGetRow(raw, raw->tmpR, i, 0);
  164.     RawImageGetRow(raw, raw->tmpG, i, 1);
  165.     RawImageGetRow(raw, raw->tmpB, i, 2);
  166.     for (j = 0; j < raw->sizeX; j++) {
  167.         *ptr++ = *(raw->tmpR + j);
  168.         *ptr++ = *(raw->tmpG + j);
  169.         *ptr++ = *(raw->tmpB + j);
  170.     }
  171.     }
  172. }
  173.  
  174. AUX_RGBImageRec *auxRGBImageLoad(char *fileName)
  175. {
  176.     rawImageRec *raw;
  177.     AUX_RGBImageRec *final;
  178.  
  179.     raw = RawImageOpen(fileName);
  180.     final = (AUX_RGBImageRec *)malloc(sizeof(AUX_RGBImageRec));
  181.     if (final == NULL) {
  182.     fprintf(stderr, "Out of memory!\n");
  183.     auxQuit();
  184.     }
  185.     final->sizeX = raw->sizeX;
  186.     final->sizeY = raw->sizeY;
  187.     RawImageGetData(raw, final);
  188.     RawImageClose(raw);
  189.     return final;
  190. }
  191.